/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistencia.controller.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ademar.concrete
 */
public class PropertyFile {

    private static PropertyFile instance = new PropertyFile();
    private static String FILE_OFF_J2ME;
    private static String FILE_OFF_LWUIT;
    private static String FILE_ON_LWUIT;

    public static PropertyFile getInstance() {
        return instance;
    }
    
    private PropertyFile() {

        String path = new File("").getAbsolutePath();
        path = path.substring(0, path.lastIndexOf("\\"));
        FILE_OFF_J2ME += path + "\\MobCat_off_j2me\\nbproject\\project.properties";
        FILE_OFF_LWUIT += path + "\\MobCat_off_lwuit\\nbproject\\project.properties";
        FILE_ON_LWUIT += path + "\\MobCat_on_lwuit\\nbproject\\project.properties";

    }

    public String getNomeExe(){
        return get(FILE_OFF_J2ME, "dist.jar");
    }

    public void setNomeExe(String nome){
       set(FILE_OFF_J2ME, "dist.jad", nome + ".jad");
       set(FILE_OFF_J2ME, "dist.jar", nome + ".jar");

       set(FILE_OFF_LWUIT, "dist.jad", nome + ".jad");
       set(FILE_OFF_LWUIT, "dist.jar", nome + ".jar");

       set(FILE_ON_LWUIT, "dist.jad", nome + ".jad");
       set(FILE_ON_LWUIT, "dist.jar", nome + ".jar");
    }

    public String getNomeApp(){
        String s = get(FILE_OFF_J2ME, "manifest.midlets");
        s = s.substring(10, s.indexOf(","));
        return s;
    }

    public void setNomeApp(String nome){
        String prop = "MIDlet-1: " + nome + ",/data/icone_portal.png,midlet.Mearch\n";
        set(FILE_OFF_J2ME, "manifest.midlets", prop);
        set(FILE_OFF_J2ME, "manifest.midlets", prop);
        set(FILE_ON_LWUIT, "manifest.midlets", prop);
    }

    private String get(String FILE, String Parametro) {
        Properties props = new Properties();
        try {
            props.load(new FileInputStream(FILE));
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return props.getProperty(Parametro);
    }


    public void set(String FILE, String Parametro, String Valor) {
        Properties prop = new Properties();
        try {
            prop.load(new FileInputStream(FILE));
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        prop.setProperty(Parametro,Valor);
        try {
            prop.store(new FileWriter(FILE), "");
        } catch (IOException ex) {
            Logger.getLogger(PropertyFile.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
